package com.project.user.service.impl;

import java.util.List;

import com.project.common.utils.TimeUuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.user.mapper.UserAddressMapper;
import com.project.user.domain.UserAddress;
import com.project.user.service.IUserAddressService;
import com.project.common.utils.str.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户收货地址 服务层实现
 *
 * @author administrator
 */
@Service
public class UserAddressServiceImpl implements IUserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 查询用户收货地址信息
     *
     * @param id 用户收货地址ID
     * @return 用户收货地址信息
     */
    @Override
    public UserAddress selectUserAddressById(Long id) {
        return userAddressMapper.selectUserAddressById(id);
    }

    /**
     * 查询用户收货地址列表
     *
     * @param userAddress 用户收货地址信息
     * @return 用户收货地址集合
     */
    @Override
    public List<UserAddress> selectUserAddressList(UserAddress userAddress) {
        return userAddressMapper.selectUserAddressList(userAddress);
    }

    /**
     * 新增用户收货地址
     *
     * @param userAddress 用户收货地址信息
     * @return 结果
     */
    @Override
    public int insertUserAddress(UserAddress userAddress) {
        userAddress.setId(TimeUuidUtil.get16UUID());
        return userAddressMapper.insertUserAddress(userAddress);
    }

    /**
     * 修改用户收货地址
     *
     * @param userAddress 用户收货地址信息
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        return userAddressMapper.updateUserAddress(userAddress);
    }

    /**
     * 删除用户收货地址对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserAddressByIds(String ids) {
        return userAddressMapper.deleteUserAddressByIds(Convert.toStrArray(ids));
    }

    /**
     * 重置用户默认地址
     *
     * @param userId 用户编号
     * @param addrId 地址编号
     * @return
     */
    @Override
    @Transactional
    public int resetUserAddressDefault(Long userId, Long addrId) {
        userAddressMapper.resetUserAddressDefault(userId);
        UserAddress userAddress = new UserAddress();
        userAddress.setId(addrId);
        userAddress.setIsDefault("1");
        return userAddressMapper.updateUserAddress(userAddress);
    }

}
